package com.hqd.jjvm.jnative.lang;

import com.hqd.jjvm.jnative.JNativeRegistry;
import com.hqd.jjvm.rtda.JThread;
import com.hqd.jjvm.rtda.LocalVars;
import com.hqd.jjvm.rtda.memory.JStringPool;
import com.hqd.jjvm.rtda.obj.JString;

public class JStringNativeRegistry extends JNativeRegistry {
    private static final JStringNativeRegistry instance = new JStringNativeRegistry();
    protected static final String CLASS_PATH = "java/lang/String";

    static {
        registry(CLASS_PATH, "intern", "()Ljava/lang/String;", JStringNativeRegistry::intern);
    }


    protected static void intern(JThread jThread) {
        LocalVars localVars = jThread.getJvmStack().getTop().getLocalVars();
        JString strRef = localVars.getRefVal(0);
        JStringPool jsp = strRef.getJClass().getConstantPool().getStringPool();
        strRef = jsp.internString(strRef.getJClass().getLoader(), strRef.getData());
        jThread.getJvmStack().pushOperandStackRefVal(strRef);
    }

    public static JStringNativeRegistry getInstance() {
        return instance;
    }
}
